package com.coinland.study;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.coinland.study.bean.DocumentBean;
import com.coinland.study.util.Contant;
import com.coinland.study.view.TabsReaderViewActivity;
import com.coinland.study.window.IWinCallback;
import com.coinland.study.window.SPUtils;
import com.coinland.study.window.WindowShowService;
import com.coinland.study.window.WindowUtil;

import java.util.List;

/**
 * 类描述:
 * 创建人:  shenZ
 * 创建时间: 2020/4/15
 * 联系方式: 252079249@qq.com
 * 修改时间: 2020/4/15
 */
public class ApplicationLifecycle implements Application.ActivityLifecycleCallbacks {
    private int started = 0;
    private SPUtils<DocumentBean> spUtils = new SPUtils<DocumentBean>();

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
        started++;
        if (started == 1) {
            Log.e("TAG", "应用在前台了！！！");
            List<DocumentBean> lists = spUtils.getList(activity, Contant.DOC_PATH, DocumentBean.class);
            if (lists.size() > 0) {
                activity.startService(new Intent(activity, WindowShowService.class));
            }
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {
        WindowUtil.getInstance().setIWinCallback(new IWinCallback<DocumentBean>() {
            @Override
            public void onCall(DocumentBean call) {
                if (activity instanceof TabsReaderViewActivity) {
                    Log.i("TBS", "当前activity----->TabsReaderViewActivity");
                } else {
                    TabsReaderViewActivity.start(activity, call);
                }
            }
        });

        if (activity instanceof TabsReaderViewActivity) {
            Log.i("TBS", "当前activity----->不作处理");
        } else {
            List<DocumentBean> lists = spUtils.getList(activity, Contant.DOC_PATH, DocumentBean.class);
            WindowUtil.getInstance().setDataOut(lists);
        }
    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        started--;
        if (started == 0) {
            Log.e("TAG", "应用在后台了！！！");
            // 判断service 是否在运行
            activity.stopService(new Intent(activity, WindowShowService.class));
        }

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

}
